﻿using System.ServiceModel.Web;
using PeoplesJukebox.Core;
using PeoplesJukebox.Core.MediaItems;
using PeoplesJukebox.Services.Helpers;

namespace PeoplesJukebox.Services
{
    /// <summary>
    /// WCF REST implementation of media player service
    /// </summary>
    public class PlayerService : IPlayerService
    {
        /// <summary>
        /// Starts playback
        /// </summary>
        /// <remarks>
        /// Should be a POST, but left as GET for compatibility
        /// </remarks>
        /// <example>
        /// http://localhost/PeoplesJukebox.Services/PlayerService.svc/start
        /// </example>
        [WebGet(UriTemplate = "start", ResponseFormat = WebMessageFormat.Xml)]
        public void Start()
        {
            PeoplesJukeboxService.Start();
        }

        /// <summary>
        /// Stops playback
        /// </summary>
        /// <remarks>
        /// Should be a POST, but left as GET for compatibility
        /// </remarks>
        /// <example>
        /// http://localhost/PeoplesJukebox.Services/PlayerService.svc/stop
        /// </example>
        [WebGet(UriTemplate = "stop", ResponseFormat = WebMessageFormat.Xml)]
        public void Stop()
        {
            PeoplesJukeboxService.Stop();
        }

        /// <summary>
        /// Resets the media player
        /// </summary>
        /// <remarks>
        /// Should be a POST, but left as GET for compatibility
        /// </remarks>
        /// <example>
        /// http://localhost/PeoplesJukebox.Services/PlayerService.svc/reset
        /// </example>
        [WebGet(UriTemplate = "reset", ResponseFormat = WebMessageFormat.Xml)]
        public void Reset()
        {
            PeoplesJukeboxService.Reset();
        }

        /// <summary>
        /// Skips playback to the next track
        /// </summary>
        /// <remarks>
        /// Should be a POST, but left as GET for compatibility
        /// </remarks>
        /// <example>
        /// http://localhost/PeoplesJukebox.Services/PlayerService.svc/skip
        /// </example>
        [WebGet(UriTemplate = "skip", ResponseFormat = WebMessageFormat.Xml)]
        public void Skip()
        {
            PeoplesJukeboxService.Skip();
        }

        /// <summary>
        /// Queues a selected track for playing
        /// </summary>
        /// <remarks>
        /// Should be a POST, but left as GET for compatibility
        /// </remarks>
        /// <example>
        /// http://localhost/PeoplesJukebox.Services/PlayerService.svc/queue/3
        /// </example>
        [WebGet(UriTemplate = "queue/{trackId}", ResponseFormat = WebMessageFormat.Xml)]
        public void Queue(string trackId)
        {
            int id = ArgumentHelper.GetId(trackId);
            if (id > -1)
            {
                PeoplesJukeboxService.AddSelectedTrack(id);
            }
        }

        /// <summary>
        /// Returns the currently-playing track
        /// </summary>
        /// <example>
        /// http://localhost/PeoplesJukebox.Services/PlayerService.svc/nowPlaying
        /// </example>
        /// <returns>Current track</returns>
        [WebGet(UriTemplate = "nowPlaying", ResponseFormat = WebMessageFormat.Xml)]
        public SelectedTrack GetCurrentTrack()
        {
            return PeoplesJukeboxService.GetCurrentTrack();
        }
    }
}
